package com.sk.weichat.adapter;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sk.weichat.AppConstant;
import com.sk.weichat.R;
import com.sk.weichat.bean.Friend;
import com.sk.weichat.bean.RoomMember;
import com.sk.weichat.db.InternationalizationHelper;
import com.sk.weichat.db.dao.RoomMemberDao;
import com.sk.weichat.helper.AvatarHelper;
import com.sk.weichat.sortlist.BaseSortModel;
import com.sk.weichat.ui.base.CoreManager;
import com.sk.weichat.ui.message.multi.RoomInfoActivity;
import com.sk.weichat.util.UiUtils;
import com.sk.weichat.view.HeadView;
import java.util.List;

/**
 * @Author: chenzl
 * @ClassName: GroupFragmentAdapter
 * @Description: 群列表适配器
 * @CreateDate: 2019/9/17 16:46
 */
public class GroupFragmentAdapter extends BaseQuickAdapter<BaseSortModel<Friend>, BaseViewHolder> {
    public GroupFragmentAdapter(List<BaseSortModel<Friend>> data) {
        super(R.layout.row_sort_friend, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, BaseSortModel<Friend> item) {

        TextView categoryTitleTv = helper.getView(R.id.catagory_title);
        categoryTitleTv.setVisibility(View.GONE);
        ImageView avatar_img = helper.getView(R.id.avatar_img);
        HeadView avatar_imgS = helper.getView(R.id.avatar_imgS);
        TextView nick_name_tv = helper.getView(R.id.nick_name_tv);
        TextView tvRoomCount = helper.getView(R.id.tv_room_count);
        TextView roles = helper.getView(R.id.roles);
        TextView des_tv = helper.getView(R.id.des_tv);
        Friend friend = item.getBean();
        avatar_img.setVisibility(View.GONE);
        avatar_imgS.setVisibility(View.VISIBLE);

        AvatarHelper.getInstance().displayAvatar(CoreManager.requireSelf(mContext).getUserId(), friend, avatar_imgS);

        /*获取群成员数量*/
//        int RoomCount = RoomMemberDao.getInstance().getRoomMember(friend.getRoomId()).size();
        int RoomCount = item.bean.getGroupMemberNum();
        // 昵称
        String nickname = TextUtils.isEmpty(friend.getRemarkName())?friend.getNickName():friend.getRemarkName();
        nick_name_tv.setText(nickname);
        tvRoomCount.setVisibility(View.VISIBLE);
        if(RoomCount==0){
            tvRoomCount.setText("("+friend.getGroupMemberNum()+")");
        }else{
            tvRoomCount.setText("("+RoomCount+")");
        }

        des_tv.setVisibility(View.VISIBLE);
        des_tv.setText(friend.getDescribe());
        RoomMember member = RoomMemberDao.getInstance().getSingleRoomMember(friend.getRoomId(),CoreManager.getSelf(mContext).getUserId());
        if (member != null) {
            if (member.getRole() == 1) {//群主
                roles.setVisibility(View.VISIBLE);
                roles.setBackgroundResource(R.drawable.bg_role1);
                roles.setText(InternationalizationHelper.getString("JXGroup_Owner"));
            } else if (member.getRole() == 2) {//管理员
                roles.setVisibility(View.VISIBLE);
                roles.setBackgroundResource(R.drawable.bg_role2);
                roles.setText(InternationalizationHelper.getString("JXGroup_Admin"));
            } else {
                roles.setVisibility(View.GONE);
            }
        }
        // 点击头像跳转详情
        avatar_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!UiUtils.isNormalClick(view)) {
                    return;
                }
                if (!friend.getUserId().equals(Friend.ID_SYSTEM_MESSAGE)) {
                    Intent intent = new Intent(mContext, RoomInfoActivity.class);
                    intent.putExtra(AppConstant.EXTRA_USER_ID, friend.getUserId());
                    mContext.startActivity(intent);
                }
            }
        });
    }
}
